<?php

namespace App\Storage;

use EasySwoole\Component\Singleton;
use EasySwoole\Component\TableManager;
use Swoole\Table;

/**
 *  所有终端
 *
 * @Author LYC 17750800820@163.com
 * @DateTime 2020-04-22 14:33:07
 *
 */
class UserStore
{
    use Singleton;
    protected $table;  // 储存终端信息的Table

    // const INDEX_TYPE_FD_ID = 1;

    /**
     * UserStore constructor.
     */
    public function __construct()
    {
        TableManager::getInstance()->add('userStore', [            
            'token' => ['type' => Table::TYPE_STRING, 'size' => 128]
        ]);

        $this->table = TableManager::getInstance()->get('userStore');
    }

    /**
     * 记录一个用户
     *
     * @Author LYC 17750800820@163.com
     * @DateTime 2020-04-22 14:51:30     
     * @param string $name
     * @param string $token
     * @return void
     */
    public function set($name, $token)
    {
        return $this->table->set($name, [                        
            'token' => $token
        ]);
    }

    /**
     * 获取一个用户
     *
     * @Author LYC 17750800820@163.com
     * @DateTime 2020-04-22 15:01:38
     * @param string $name
     * @return void
     */
    public function get($name)
    {
        $info = $this->table->get($name);
        return is_array($info) ? $info : null;
    }
   
    /**
     * 直接获取当前的表
     * @return Table|null
     */
    public function table()
    {
        return $this->table;
    }
}
